<?php

namespace App\Exceptions;

use Throwable;
use App\Models\Reasonable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Client\HttpClientException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

class Handler extends ExceptionHandler
{
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {

        // 在发生没有被捕获的异常或者错误后，laravel 框架的错误处理会用使用 App\Exceptions\Handler 进行处理
        $this->reportable(function (Throwable $e) {
            writeLog("内部错误: " . $e->getMessage());
            return error("服务器内部错误", 500);
        });
    }

    public function render($request, Throwable $exception)
    {
        $table = sprintf("reasonable_%s", date("Ymd"));
        createReasonableTable($table);
        DB::table($table)->insert([
            'error_code' => $exception->getCode(),
            'error_message' => $exception->getMessage(),
            'error_file_path' => $exception->getFile(),
            'error_file_line' => $exception->getLine(),
            'error_trace' => toJson($exception->getTrace()),
        ]);

        // if ($exception instanceof NotFoundHttpException) {
        //     return Route::respondWithRoute('fallback');
        //     return error($exception->getMessage(), 404);
        // }

        // if ($exception instanceof MethodNotAllowedHttpException) {
        //     // return Route::respondWithRoute('fallback');
        //     return error($exception->getMessage(), 404);
        // }

        // if ($exception instanceof ModelNotFoundException) {
        //     $message = 'Entry for '.str_replace('App\\', '', $exception->getModel()).' not found';
        //     return error($exception->getMessage(), 404);
        // }

        // if ($exception instanceof ThrottleRequestsException) {
        //     return error("达到调用次数", 10000);
        // }

        if( isDebug() ) {
            return error( $exception->getMessage(), $exception->getCode(), $exception->getTrace() );
        } 

        if($exception instanceof HttpException) {
            return error( "Internal Server Error", 500);
        }

        if($exception instanceof BadRequestHttpException) {
            return error( $exception->getMessage(), $exception->getCode());
        }

        return error( $exception->getMessage(), $exception->getCode());
        // return parent::render($request, $exception);
    }
}
